package etc;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import javax.swing.ImageIcon;

import runner.EnemyTemplate;
import towers.Projectile;
import towers.TowerTemplate;
import buffs.Buff;
import buffs.FreezeBuff;

public class Parser {

	public static Set<TowerTemplate> getTowers() {
		Map<String, Class<? extends Buff>> buffs = new HashMap<String, Class<? extends Buff>>();
		buffs.put("freeze", FreezeBuff.class);
		
		Map<String, Class<? extends Projectile>> projs = new HashMap<String, Class<? extends Projectile>>();
		projs.put("normal", Projectile.class);
		projs.put("splash", SplashProj.class);
		
		Set<TowerTemplate> set = new HashSet<TowerTemplate>();
		Scanner scan=null;
		try {
			scan = new Scanner(new File("data/towers.txt"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.exit(1);
		}
		Map<String, TowerTemplate> s2t = new HashMap<String, TowerTemplate>();
		Map<TowerTemplate, Set<String>> upg = new HashMap<TowerTemplate, Set<String>>();
		
		String key,val;
		while (scan.hasNextLine()) {
			String line = scan.nextLine();
			if (line.startsWith(".") || line.startsWith("-")) {
				TowerTemplate t = new TowerTemplate();
				if (line.startsWith("-"))
					set.add(t);
				t.name = line.substring(1);
				t.icon = new ImageIcon("data/towers/"+ t.name.replace(' ', '_') +".png");
				t.buffs = new HashMap<Class<? extends Buff>, String>();
				t.projectile = Projectile.class;
				while (scan.hasNextLine()) {
					line = scan.nextLine();
					if (line.split("=").length != 2) break;
					key=line.split("=")[0];
					val=line.split("=")[1];
					if (key.equals("power")) {
						t.power = Integer.parseInt(val);
					} else if (key.equals("reload")) {
						t.reload = Integer.parseInt(val);
					}else if (key.equals("range")) {
						t.range = Integer.parseInt(val);
					}else if (key.equals("cost")) {
						t.cost = Integer.parseInt(val);
					} else if (key.equals("upgrades")) {
						Set<String> s = new HashSet<String>();
						for (String ss: val.split(",")) {
							s.add(ss);
						}
						upg.put(t, s);
					} else if (key.equals("buffs")) {
						for (String ss: val.split(",")) {
							String[] t1 = ss.split(" ", 2);
							String buffname = t1[0];
							t.buffs.put(buffs.get(buffname), t1[1]);
						}
					} else if (key.equals("projectile")) {
						
					}
				}
				s2t.put(t.name, t);
			}
		}
		for (TowerTemplate t: upg.keySet()) {
			Set<TowerTemplate> upgrades = new HashSet<TowerTemplate>();
			for (String s: upg.get(t)) {
				upgrades.add(s2t.get(s));
			}
			t.upgrades = upgrades;
		}
		return set;
	}
	
	public static List<EnemyTemplate> getEnemies() {
		List<EnemyTemplate> ret = new ArrayList<EnemyTemplate>();
		
		Scanner scan=null;
		try {
			scan = new Scanner(new File("data/enemies.txt"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.exit(1);
		}
			
		while (scan.hasNextLine()) {
			String line = scan.nextLine();
			if (line.isEmpty())
				continue;
			
			EnemyTemplate t = new EnemyTemplate();
			t.name = line;
			t.icon = new ImageIcon("data/enemies/"+t.name.replace(' ', '_')+".png");
			
			while (scan.hasNextLine()) {
				line = scan.nextLine();
				if (line.split("=").length != 2)
					break;
				
				String key = line.split("=")[0];
				String val = line.split("=")[1];
				
				if (key.equals("armor")) {
					t.armor = Integer.parseInt(val);
					
				} else if (key.equals("hp")) {
					t.maxhp = Integer.parseInt(val);
					
				} else if (key.equals("bounty")) {
					t.bounty = Integer.parseInt(val);
					
				} else if (key.equals("speed")) {
					t.speed = Integer.parseInt(val);

				}
			}
			ret.add(t);
			
		}
		return ret;
	}
}
